import json
from datetime import date, datetime
from bson import ObjectId


class JsonExtendEncoder(json.JSONEncoder):
    """
        This class provide an extension to json serialization for datetime/date.
    """

    def default(self, o):
        """
            provide a interface for datetime/date
        """
        # if isinstance(o, datetime):
        #     return o.strftime('%Y-%m-%d %H:%M:%S')
        # elif isinstance(o, date):
        #     return o.strftime('%Y-%m-%d')
        if isinstance(o, (datetime, date)):
            return o.isoformat()
        elif isinstance(o, ObjectId):
            return str(o)
        else:
            return json.JSONEncoder.default(self, o)
